//
//  MapCoordTransfromDemoViewController.m
//  FMDemoBaseMap
//
//  Created by fengmap on 16/10/18.
//  Copyright © 2016年 fengmap. All rights reserved.
//
//  坐标转换
//  简介：屏幕坐标与地理坐标的相互转换；

#import "MapCoordTransfromDemoViewController.h"
#import "FMMapKit.h"

@interface MapCoordTransfromDemoViewController () <FMKMapViewDelegate>
{
    FMKMapView *_mapView;
    UITextView *_textView;
    /// 地图图片标注层
    FMKImageLayer *_imageLayer;
}

@end

@implementation MapCoordTransfromDemoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 离线地图加载
    NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"jlai-cyds-museum02" ofType:@"fmap"];
    _mapView = [[FMKMapView alloc] initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-64) path:dataPath delegate:self];
       // _mapView.enable3D = NO;///
    
    [self.view addSubview:_mapView];
    
    
    NSString *themePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"jlai-cyds-museum02/jlai-cyds-museum02.theme"];
    [_mapView setThemeWithLocalPath:themePath];
    
    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 120, [UIScreen mainScreen].bounds.size.width, 120)];
    [self.view addSubview:textView];
    textView.backgroundColor = [UIColor clearColor];
    textView.editable = NO;
    textView.font = [UIFont systemFontOfSize:15];
    textView.text = @"点击地图";
    _textView = textView;
    
    // 通过groupID在此层上初始化图片层
    _imageLayer = [[FMKImageLayer alloc] initWithGroupID:@"1"];
    // 添加图层到map上
    [_mapView.map addLayer:_imageLayer];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - FMKMapViewDelegate
- (void)mapViewDidFinishLoadingMap:(FMKMapView *)mapView {
    NSLog(@"地图加载完成");
}
- (void)mapViewDidFailLoadingMap:(FMKMapView *)mapView withError:(NSError *)error {
    NSLog(@"地图加载失败-%@", error);
}
/**
 *  当单击地图时
 *
 *  @param mapView mapView
 *  @param point   单击点
 */
- (void)mapView:(FMKMapView *)mapView didSingleTapWithPoint:(CGPoint)point
{
    // 屏幕坐标转换为地理坐标
    FMKGeoCoord coord = [_mapView coverPoint:point];
    //FMKGeoCoord
    
    
    
    
    // 地理坐标转换为屏幕坐标
    CGPoint p = [_mapView coverCoord:coord zType:FMKMAPCOORDZ_EXTENT];
    
    _textView.text = [NSString stringWithFormat:@"当前楼层(groupId)：groupId=%d\n原始屏幕坐标(point)：x=%.2f, y=%.2f\n转换后的地理坐标(mapCoord)：x=%.2f, y=%.2f\n转换后的屏幕坐标(point)：x=%.2f, y=%.2f", coord.groupID, point.x, point.y, coord.mapPoint.x, coord.mapPoint.y, p.x, p.y];
    
    // 移除图片标注层上所有标注物
    [_imageLayer removeAll];
    // 初始化图片标注
    //FMKImageMarker *imageMarker = [[FMKImageMarker alloc] initWithImage:[UIImage imageNamed:@"blueImageMarker"] Coord:coord.mapPoint];
    
    FMKImageMarker *imageMarker = [[FMKImageMarker alloc] initWithImage:[UIImage imageNamed:@"blueImageMarker"] Coord:FMKMapPointMake(13943498, 5437709)];
    
    
    // 设置图片标注大小
    imageMarker.imageSize = CGSizeMake(30, 30);
    // 设置图片标注位于地图上的位置
    imageMarker.offsetMode = FMKImageMarker_MODELTOP;
    //imageMarker.offsetMode = FMKImageMarker_BILLBOARD;
    
    
    FMKLocationMarker * locaitonMaker = [[FMKLocationMarker alloc]initWithPointerImageName:@"pointer0" DomeImageName:(@"dome.png")];
    
   // [locaitonMaker locateWithGeoCoord:FMKGeoCoordMake(1, FMKMapPointMake(13943498, 5437709))];//FMKGeoCoordMake(1, FMKGeoCoordMake(1, FMKMapPointMake(0, 0));(0, 0));
    [locaitonMaker locateWithGeoCoord:FMKGeoCoordMake(1, FMKMapPointMake(13943478.9942, 5437721.4263))];
    
    [_mapView.map.locationLayer addMarker:locaitonMaker];
    locaitonMaker.size = CGSizeMake(32, 32);
    
    
    
    ////////////////////////////////////
    
    FMKLocationMarker * locaitonMaker1 = [[FMKLocationMarker alloc]initWithPointerImageName:@"pointer0" DomeImageName:(@"dome.png")];
    
    // [locaitonMaker locateWithGeoCoord:FMKGeoCoordMake(1, FMKMapPointMake(13943498, 5437709))];//FMKGeoCoordMake(1, FMKGeoCoordMake(1, FMKMapPointMake(0, 0));(0, 0));
  [locaitonMaker1 locateWithGeoCoord:FMKGeoCoordMake(1, FMKMapPointMake(13943525.0779, 5437721.4259))];
    
    [_mapView.map.locationLayer addMarker:locaitonMaker1];
    locaitonMaker.size = CGSizeMake(32, 32);
    
    
    
    
   // _mapView
    
    // 添加图片标注物到图片标注层
    // [_imageLayer addMarker:imageMarker];
    
}

@end
